//
//  WiteBoxViewController.m
//  WiteBox
//
//  Created by John Blanco on 6/26/09.
//  Copyright 2009 Effective UI. All rights reserved.
//

#import "WiteBoxViewController.h"

#import "TestLocationModel.h"
#import "TestCaseRun.h"
#import "WiteBoxTestsViewController.h"
#import "WiteBoxStatisticsViewController.h"
#import "WiteBoxStyles.h"

@implementation WiteBoxViewController

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		[self startUnitTesting];
    }
    return self;
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];

	self.title = [NSString stringWithFormat:@"%@ v%@", [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleName"], [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleVersion"]];
	self.view.backgroundColor = [WiteBoxStyles backgroundColor];

	witeBoxStatisticsHeaderViewController = [[WiteBoxStatisticsViewController alloc] init];
	witeBoxStatisticsHeaderViewController.view.frame = CGRectMake(0, 0, 320, 70);

	// table
	tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 100, 300, 326) style:UITableViewStyleGrouped];
	tableView.delegate = self;
	tableView.dataSource = self;
	tableView.backgroundColor = [UIColor clearColor];
	tableView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
	
	[self.view addSubview:witeBoxStatisticsHeaderViewController.view];
	[self.view addSubview:tableView];

	self.navigationItem.backBarButtonItem =
	[[UIBarButtonItem alloc] initWithTitle:@"Back"
									 style: UIBarButtonItemStyleBordered
									target:nil
									action:nil];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
	[tableView release];
	[witeBoxTestsViewController release];
	[witeBoxStatisticsHeaderViewController release];
    [super dealloc];
}

#pragma mark -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [[[TestLocationModel sharedInstance] testCaseRuns] count];
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TestCaseCell";
	
    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		cell.backgroundColor = [WiteBoxStyles cellBackgroundColor];
		cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
		cell.detailTextLabel.font = [UIFont systemFontOfSize:12.0];
		cell.textLabel.textColor = [WiteBoxStyles cellTextColor];
		cell.detailTextLabel.textColor = [WiteBoxStyles cellTextDetailColor];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
		UILabel *countLabel = [[[UILabel alloc] initWithFrame:CGRectMake(160.0, 15.0, 100.0, 14.0)] autorelease];
		countLabel.backgroundColor = [UIColor clearColor];
		countLabel.font = [UIFont boldSystemFontOfSize:12.0];
		countLabel.textAlignment = UITextAlignmentRight;
		countLabel.tag = 746;
		
		[cell addSubview:countLabel];
	}

	TestCaseRun *testCaseRun = (TestCaseRun *)[[[TestLocationModel sharedInstance] testCaseRuns] objectAtIndex:indexPath.row];
	UILabel *countLabel = (UILabel *)[cell viewWithTag:746];
	cell.textLabel.text = [testCaseRun testCaseClassName];
	cell.detailTextLabel.text = [NSString stringWithFormat:@"(%0.2fs)", [testCaseRun durationOfAllRuns]];
	
	if ([testCaseRun executedTests] == 0) {
		countLabel.text = [NSString stringWithFormat:@"no tests"];
	} else {
		countLabel.text = [NSString stringWithFormat:@"%i of %i", [testCaseRun passingTests], [testCaseRun executedTests]];
	}
	
	if ([testCaseRun passingTests] == 0 && [testCaseRun failingTests] == 0) {
		countLabel.textColor = [UIColor blackColor];
	} else if ([testCaseRun failingTests] == 0) {
		countLabel.textColor = [WiteBoxStyles greenCorrectDarkerContrast];
	} else {
		countLabel.textColor = [WiteBoxStyles redWrongDarkerContrast];
	}
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if (!witeBoxTestsViewController) {
		witeBoxTestsViewController = [[WiteBoxTestsViewController alloc] initWithStyle:UITableViewStyleGrouped];
	}

	witeBoxTestsViewController.testCaseRun = (TestCaseRun *)[[[TestLocationModel sharedInstance] testCaseRuns] objectAtIndex:indexPath.row];
	
	[self.navigationController pushViewController:witeBoxTestsViewController animated:YES];
}

#pragma mark -

- (void)startUnitTesting {
	[self performSelectorInBackground:@selector(doUnitTesting) withObject:nil];
}

- (void)doUnitTesting {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	TestLocationModel *testLocationModel = [TestLocationModel sharedInstance];
	
	[testLocationModel setTarget:self action:@selector(unitTestPerformed:)];
	[testLocationModel collectAllTestCaseClasses];
	[testLocationModel scheduleAllTestCases];
	[testLocationModel runAllTestCases];
	
	[pool release];
}

- (void)unitTestPerformed:(NSNumber *)isComplete {
	[witeBoxStatisticsHeaderViewController updateDisplay];
	
	[tableView reloadData];
}

@end
